/**
 * Filename: OfficialListAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/6 16:23
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.AlbumStatusActivity;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.util.List;

public class OfficialListAdapter extends RecyclerView.Adapter<OfficialListAdapter.ViewHolder> {

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private Context context;
    private List<YNAlbum> datas;
    private ImageLoader imageLoader;
    private View mHeaderView;

    public OfficialListAdapter(Context context, List<YNAlbum> datas) {
        this.context = context;
        this.datas = datas;
        imageLoader = ImageLoader.getInstance();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_official_item_album;                            //列表View
        ImageView iv_official_item_album_bg;                            //组背景图
        TextView tv_official_album_name;                            //组名
        TextView tv_official_album_users;                            //组人数
        TextView tv_official_album_photos;                            //组相片数
        ImageView iv_official_join;                                  //加入
        RelativeLayout rl_official_join;

        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            ll_official_item_album = (LinearLayout) itemView
                    .findViewById(R.id.ll_official_item_album);
            iv_official_item_album_bg = (ImageView) itemView
                    .findViewById(R.id.iv_official_item_album_bg);
            rl_official_join = (RelativeLayout) itemView
                    .findViewById(R.id.rl_official_join);
            iv_official_join = (ImageView) itemView
                    .findViewById(R.id.iv_official_join);
            tv_official_album_name = (TextView) itemView
                    .findViewById(R.id.tv_official_album_name);
            tv_official_album_users = (TextView) itemView
                    .findViewById(R.id.tv_official_album_users);
            tv_official_album_photos = (TextView) itemView
                    .findViewById(R.id.tv_official_album_photos);
        }

        public void bindData(final YNAlbum listItems, final int point) {
            if (listItems != null) {
                String paths = YNUrlHelper.getOfficialUrl(context, listItems.getGpic());
                imageLoader.displayImage(paths,
                        new ImageViewAware(iv_official_item_album_bg, false),
                        ImageOptHelper.getBgOptions());
                tv_official_album_name.setText(listItems.getGname());
                tv_official_album_users.setText(listItems.getGnum());
                tv_official_album_photos.setText(String.valueOf(listItems.getGpicnum()));

//                if(1==listItems.getIsInAlbum()){
//                    rl_official_join.setVisibility(View.GONE);
//                }else{
//                    rl_official_join.setVisibility(View.VISIBLE);
//                }
//                iv_official_join.setOnClickListener(new View.OnClickListener(){
//                    @Override
//                    public void onClick(View v) {
//                        YNSingleton.getInstance().updateIsOfficialAlbum(listItems,1);
//                        datas.get(point).setIsInAlbum(1);
//                        rl_official_join.setVisibility(View.GONE);
//                    }
//                });
                // 点击列表
                ll_official_item_album.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        YNSingleton.getInstance().setCurAlbum(listItems);
                        ((BaseApplication) context.getApplicationContext()).setCurGid(listItems.getGroupid());
                        Intent intent = new Intent(context, AlbumStatusActivity.class);
                        intent.putExtra(ACTIONs.aAddData, false);
                        intent.putExtra(CommonConstants.YN_ALBUM, listItems);
                        context.startActivity(intent);
                    }
                });
            }
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private YNAlbum getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_official_album, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        YNAlbum listItems = getItem(pos);

        holder.bindData(listItems, pos);
        holder.itemView.setTag(listItems);
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    public void isInAlbumSwitch() {
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getGroupid() == YNSingleton.getInstance().getCurAlbum().getGroupid()) {
                datas.get(i).setIsInAlbum(0);
                this.notifyItemChanged(i);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? datas.size() : datas.size() + 1;
    }
}
